package com.orphan.marketBack.service.impl;

import com.orphan.marketBack.dao.GoodsMapper;
import com.orphan.marketBack.dao.GoodsShowMapper;
import com.orphan.marketBack.dao.StockMapper;
import com.orphan.marketBack.po.*;
import com.orphan.marketBack.service.PurchaseGoodsService;
import com.orphan.marketBack.service.PurchaseService;
import com.orphan.marketBack.service.StockService;
import com.orphan.marketBack.util.DateUtil;
import com.orphan.marketBack.vo.ResultVO;
import com.orphan.marketBack.vo.StockGoods;
import com.orphan.util.dto.GoodsDto;
import com.orphan.util.dto.PurchaseGoodDto;
import com.orphan.util.dto.PurchaseOrderDto;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StockServiceImpl implements StockService {
    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsShowMapper goodsShowMapper;
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private PurchaseGoodsService purchaseGoodsService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public ResultVO getStockGoods() {
        List<Stock> stock = stockMapper.getStock();
        List<Goods> goods = goodsMapper.getGoods();
        List<GoodsShow> goodsShow = goodsShowMapper.getGoodsShow();
        List<StockGoods> stockGoods = new ArrayList<>();
        for (int i = 0; i < stock.size(); i++) {
            for (int j = 0; j < goods.size(); j++) {
                if (stock.get(i).getStockId().equals(goods.get(j).getStockId())){
                    StockGoods stockGoods1 = new StockGoods();
                    stockGoods1.setStockId(stock.get(i).getStockId());
                    stockGoods1.setGoodsName(stock.get(i).getGoodsName());
                    stockGoods1.setGoodsNum(stock.get(i).getStockNum());
                    stockGoods1.setGoodsSize(goods.get(j).getGoodsSize());
                    stockGoods1.setGoodsType(goods.get(j).getGoodsType());
                    stockGoods1.setGoodsId(goods.get(j).getGoodsId());
                    stockGoods1.setGoodsPrice(goods.get(j).getGoodsPrice());

                    stockGoods.add(stockGoods1);
                }
            }
        }
        for (int i = 0; i < stockGoods.size(); i++) {
            for (int j = 0; j < goodsShow.size(); j++) {
                if(stockGoods.get(i).getStockId().equals(goodsShow.get(j).getStockId())){
                    stockGoods.get(i).setGoodsShow(goodsShow.get(j).getGoodsShow());
                    stockGoods.get(i).setGoodsPromotion(goodsShow.get(j).getGoodsPromotion());
                }
            }
        }

        ResultVO resultVO = new ResultVO();
        resultVO.setTotal(stockGoods.size());
        resultVO.setData(stockGoods);

        return resultVO;
    }

    @Override
    public boolean updateGoodsUp(String stockId) {
        int i = goodsShowMapper.updateGoodsUp(stockId);
        if(i > 0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean updateGoodsDown(String stockId) {
        int i = goodsShowMapper.updateGoodsDown(stockId);
        if(i > 0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Stock selectStock(String stockId) {
        Stock stock = stockMapper.selectStock(stockId);
        return stock;
    }

    @Override
    public GoodsDto getGoods(String stockId) {
        Stock stock = stockMapper.selectStock(stockId);
        Goods goods = goodsMapper.selectGoods(stockId);

        GoodsDto gd = new GoodsDto();
        gd.setGoodName(stock.getGoodsName());
        gd.setGoodNum(stock.getStockNum());
        gd.setGoodPrice(goods.getGoodsPrice());
        gd.setGoodSize(goods.getGoodsSize());
        gd.setGoodType(goods.getGoodsType());
        gd.setGoodTag(1);

        return gd;
    }

    @Override
    public boolean updateStock(OrderReturn orderReturn) {
        int i = 0;
        if(orderReturn.getGoodsOrderStatus() == 2){
            i = stockMapper.updateStock(orderReturn);
        }
        if(i > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean deleteStock(String goodsName) {
        int i = stockMapper.deleteStock(goodsName);
        if(i > 0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean deductionStock(OrderGoods goods) {
        int i = stockMapper.deductionStock(goods);
        if(i > 0){
            Stock stock = stockMapper.selectStockByName(goods);
//            if(stock.getStockNum() < 5){
//
//                boolean flag = false;
//                Purchase purchase = new Purchase();
//                PurchaseGood good = new PurchaseGood();
//                PurchaseOrderDto pod = new PurchaseOrderDto();
//                PurchaseGoodDto pgd = new PurchaseGoodDto();
//                //把采购单信息添加到数据库中保存
//                purchase.setPurchaseId(goods.getOrderId() + "PU");
//
//                long l = System.currentTimeMillis();
//                String s = DateUtil.timeStamp2Date(String.valueOf(l), null);
//
//                purchase.setPurchaseTime(s);
//                purchase.setPurchaseStatus("审核中");
//
//                boolean b1 = purchaseService.insert(purchase);
//
//                //把采购单中的需要采购的商品保存到数据库中
//                good.setPurchaseId(goods.getOrderId() + "PU");
//                good.setGoodsName(goods.getGoodsType());
//                good.setGoodsSize(goods.getGoodsPicture());
//                good.setGoodsNum(String.valueOf(goods.getGoodsCount()));
//                boolean b2 = purchaseGoodsService.insert(good);
//
//                if (b1 && b2){
//                    flag = true;
//                }
//
//                pod.setOrderNum(goods.getOrderId() + "PU");
//                pod.setOrderTime(s);
//                pgd.setGoodName(goods.getGoodsType());
//                pgd.setGoodNum(String.valueOf(goods.getGoodsCount()));
//                pgd.setGoodSize(goods.getGoodsPicture());
//                List<PurchaseGoodDto> list = new ArrayList<>();
//                list.add(pgd);
//                pod.setGoods(list);
//
//                rabbitTemplate.convertAndSend("purchaseExchange","purchase",pod);
//
//            }
            return true;
        }else{
            return false;
        }
    }

}
